from langchain_core.runnables.graph import MermaidDrawMethod
from langgraph.graph import Graph

# 下载zhipuai,创建密钥
from langchain_openai import ChatOpenAI
def qw_model():
    return ChatOpenAI(
        model='qwen-max-0919',
        base_url='https://dashscope.aliyuncs.com/compatible-mode/v1',
        api_key='sk-debee146f82244268914dd2e3d98761b',
        temperature=0.7,
        top_p=0.9
    )

llm = qw_model()
def ask(user_input:str):
    resp=llm.invoke(user_input)
    return resp.content

def answer(resp:str):
    return f"Assistant Say:{resp}"

workflow = Graph()

workflow.add_node("ask_node", ask)
workflow.add_node("answer_node", answer)
workflow.add_edge("ask_node", "answer_node")

workflow.set_entry_point("ask_node")
workflow.set_finish_point("answer_node")

app = workflow.compile()
# res = app.invoke("介绍下李白")
# print(res)

#图可视化
# app.get_graph().print_ascii()
graph_img = app.get_graph().draw_mermaid_png(
    draw_method=MermaidDrawMethod.API,
)
with open("graph_1.png", "wb") as f:
    f.write(graph_img)

# print(type(graph_img))